package com.regent.central.bill.web.sale_order;

import com.regent.central.bill.core.CreateGoodsRemark;
import com.regent.central.bill.core.ReturnGoodsRemark;
import com.regent.central.bill.core.SaleOrderRemark;
import com.regent.central.bill.service.CreateGoodsRemarkService;
import com.regent.central.bill.service.ReturnGoodsRemarkService;
import com.regent.central.bill.service.SaleOrderRemarkService;
import com.regent.central.infrastructure.common.TimeKit;
import com.regent.central.infrastructure.common.Uid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 销售单备注控制器
 *
 * @author 杨昌沛 646742615@qq.com
 * @date 2018-06-27 16:16
 **/
@Controller
@RequestMapping("/sale-orders")
public class SaleOrderRemarkController {

    @Resource
    private SaleOrderRemarkService saleOrderRemarkService;

    @Resource
    private CreateGoodsRemarkService createGoodsRemarkService;

    @Resource
    private ReturnGoodsRemarkService returnGoodsRemarkService;

    /**
     * 销售单备注创建
     *
     * @param request 销售单备注创建请求
     * @return 销售单备注创建结果
     */
    @RequestMapping(value = "/remarks", method = RequestMethod.POST)
    @ResponseBody
    public SaleOrderRemarkCreateResponse addSaleOrderRemark(
            @RequestBody SaleOrderRemarkCreateRequest request
    ) {
        SaleOrderRemark remark = new SaleOrderRemark();
        remark.setId(Uid.random());
        remark.setCentralBillSaleOrderId(request.getSaleOrderId());
        remark.setRemark(request.getRemark());
        remark.setDateRemarked(request.getDateRemarked() == null ? new Date() : TimeKit.parse(request.getDateRemarked(), "yyyy-MM-dd HH:mm:ss"));
        remark.setUserCreated(request.getUserId());
        remark.setSequence(0);
        saleOrderRemarkService.insertSelective(remark);
        return new SaleOrderRemarkCreateResponse();
    }

    /**
     * 销售单备注删除
     *
     * @param request 销售单备注删除请求
     * @return 销售单备注删除结果
     */
    @RequestMapping(value = "/remarks", method = RequestMethod.DELETE)
    @ResponseBody
    public SaleOrderRemarkDeleteResponse deleteSaleOrderRemark(
            SaleOrderRemarkDeleteRequest request
    ) {
        saleOrderRemarkService.hide(request.getSaleOrderRemarkId(), request.getUserId());
        return new SaleOrderRemarkDeleteResponse();
    }

    /**
     * 销售单开单货品备注创建
     *
     * @param request 销售单开单货品备注创建请求
     * @return 销售单开单货品备注创建结果
     */
    @RequestMapping(value = "/create-goods/remarks", method = RequestMethod.POST)
    @ResponseBody
    public SaleOrderActionGoodsRemarkCreateResponse addSaleOrderCreateGoodsRemark(
            @RequestBody SaleOrderActionGoodsRemarkCreateRequest request
    ) {
        CreateGoodsRemark remark = new CreateGoodsRemark();
        remark.setId(Uid.random());
        remark.setBossUnitId(request.getBossUnitId());
        remark.setUnitId(request.getUnitId());
        remark.setCentralActionCreateGoodsId(request.getSaleOrderId());
        remark.setRemark(request.getRemark());
        remark.setUserCreated(request.getUserId());
        remark.setSequence(0);
        createGoodsRemarkService.insertSelective(remark);
        return new SaleOrderActionGoodsRemarkCreateResponse();
    }

    /**
     * 销售单开单货品备注删除
     *
     * @param request 销售单开单货品备注删除请求
     * @return 销售单开单货品备注删除结果
     */
    @RequestMapping(value = "/create-goods/remarks", method = RequestMethod.DELETE)
    @ResponseBody
    public SaleOrderActionGoodsRemarkDeleteResponse deleteSaleOrderCreateGoodsRemark(
            SaleOrderActionGoodsRemarkDeleteRequest request
    ) {
        createGoodsRemarkService.hide(request.getActionGoodsRemarkId(), request.getUserId());
        return new SaleOrderActionGoodsRemarkDeleteResponse();
    }

    /**
     * 销售单退货备注创建
     *
     * @param request 销售单退货备注创建请求
     * @return 销售单退货备注创建结果
     */
    @RequestMapping(value = "/return-goods/remarks", method = RequestMethod.POST)
    @ResponseBody
    public SaleOrderActionGoodsRemarkCreateResponse addSaleOrderReturnGoodsRemark(
            @RequestBody SaleOrderActionGoodsRemarkCreateRequest request
    ) {
        ReturnGoodsRemark remark = new ReturnGoodsRemark();
        remark.setId(Uid.random());
        remark.setBossUnitId(request.getBossUnitId());
        remark.setUnitId(request.getUnitId());
        remark.setCentralActionReturnGoodsId(request.getSaleOrderId());
        remark.setRemark(request.getRemark());
        remark.setUserCreated(request.getUserId());
        remark.setSequence(0);
        returnGoodsRemarkService.insertSelective(remark);
        return new SaleOrderActionGoodsRemarkCreateResponse();
    }

    /**
     * 销售单退货备注删除
     *
     * @param request 销售单退货备注删除请求
     * @return 销售单退货备注删除结果
     */
    @RequestMapping(value = "/return-goods/remarks", method = RequestMethod.DELETE)
    @ResponseBody
    public SaleOrderActionGoodsRemarkDeleteResponse deleteSaleOrderReturnGoodsRemark(
            SaleOrderActionGoodsRemarkDeleteRequest request
    ) {
        returnGoodsRemarkService.hide(request.getActionGoodsRemarkId(), request.getUserId());
        return new SaleOrderActionGoodsRemarkDeleteResponse();
    }

}
